package com.qc.ayouth.shop.dbs;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: tony
 * @CreateDate: 2019/9/11
 */
public class MybatisPlusGenerator {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/ayouthshop?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String AUTHOR = "Haojg";

    //包名，会按照com/example/demo这种形式生成类
    private static final String PACKAGE = "com.qc.ayouth.shop.dbs";


    public static void main(String[] args) {
//        generateByTables(false, new String[]{"t_system_admin", "t_system_menu", "t_system_role", "t_system_role_menu", "t_system_admin_role"});
//        generateByTables(false, new String[]{"t_users", "t_user_collect", "t_user_comment", "t_user_feedback", "t_user_footprint",
//         "t_user_search_history", "t_user_address"});
        generateByTables(false, new String[]{"t_shop_goods", "t_shop_goods_attribute", "t_shop_goods_product", "t_shop_goods_specification",
                "t_shop_brand", "t_shop_issue", "t_shop_keyword", "t_shop_order", "t_shop_order_goods", "t_shop_topic", "t_shop_ad",
                "t_shop_aftersale", "t_shop_region", "t_shop_cart", "t_shop_category"});
    }

    private static void generateByTables(boolean serviceNameStartWithI, String... tableNames) {

        String projectPath;
        String projectPathOld = System.getProperty("user.dir");
//        String projectPathOld = "D:\\log";
        projectPath = projectPathOld.replace("\\","/");

        List<TableFill> tableFillList = new ArrayList<>();
        TableFill createUserFill = new TableFill("create_by", FieldFill.INSERT);
        TableFill createTimeFill = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateUserFill = new TableFill("update_by", FieldFill.UPDATE);
        TableFill updateTimeFill = new TableFill("update_time", FieldFill.UPDATE);
        tableFillList.add(createUserFill);
        tableFillList.add(createTimeFill);
        tableFillList.add(updateUserFill);
        tableFillList.add(updateTimeFill);

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(DB_URL)
                .setUsername(USER_NAME)
                .setPassword(PASSWORD)
                .setDriverName(DRIVER);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setNaming(NamingStrategy.underline_to_camel)
                .setTableFillList(tableFillList)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组

        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false)
                .setAuthor(AUTHOR)
                .setOutputDir(projectPath + "/ayouth-dbs/src/main/java")
                .setServiceImplName("%sServiceImpl")
                .setOpen(false);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);//不生成controller
//        templateConfig.setService(null);

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setTemplate(templateConfig)
//                .setTemplateEngine(new BeetlTemplateEngine())
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(PACKAGE)
                                .setMapper("mapper")
                                .setEntity("entity")
                                .setService("service")
                                .setServiceImpl("service.impl")
                ).execute();
    }
}

